package com.cyf.plc.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import com.cyf.library.util.logi
import com.cyf.plc.constant.Constants
import com.cyf.plc.constant.Constants.Companion.ATTR_HUMIDITY
import com.cyf.plc.constant.Constants.Companion.ATTR_POWER
import com.cyf.plc.constant.Constants.Companion.ATTR_SET_TEMP
import com.cyf.plc.constant.Constants.Companion.ATTR_TEMP
import com.cyf.plc.constant.Constants.Companion.PANEL_ATTR_IDS
import com.cyf.plc.constant.Constants.Companion.UNDERLINE
import com.cyf.plc.data.repository.DatabaseRepository
import com.cyf.plc.model.PlcAttribute
import com.cyf.plc.model.PlcFloor
import com.cyf.plc.model.PlcRoom
import com.cyf.plc.model.PlcSubAttribute
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject

@ExperimentalCoroutinesApi
class PlcViewModel @Inject constructor(private val databaseRepository: DatabaseRepository) :
    ViewModel() {

    fun getPlcSubAttributes() =
        databaseRepository.getPlcSubAttributes().flowOn(Dispatchers.IO).asLiveData()

    fun getPlcSubAttributes(attributeIds: Array<String>) =
        databaseRepository.getPlcSubAttributes(attributeIds).flowOn(Dispatchers.IO).asLiveData()

    fun getFloorWithRooms() =
        databaseRepository.getFloorWithRooms().flowOn(Dispatchers.IO).asLiveData()

    fun deleteRooms(plcRooms: List<PlcRoom>) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                databaseRepository.deleteRooms(plcRooms)
            }
        }
    }

    fun saveRoom(plcRoom: PlcRoom) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                val id = databaseRepository.saveRoom(plcRoom)

                //新增房间属性
                "add room id:$id".logi()
                val plcAttributes = ArrayList<PlcAttribute>()
                plcAttributes.add(
                    PlcAttribute(
                        id.toString() + UNDERLINE + PANEL_ATTR_IDS[0],
                        0,
                        Constants.VW,
                        ATTR_POWER,
                        "开关"
                    )
                )
                plcAttributes.add(
                    PlcAttribute(
                        id.toString() + UNDERLINE + PANEL_ATTR_IDS[1],
                        0,
                        Constants.VW,
                        ATTR_TEMP,
                        "温度"
                    )
                )
                plcAttributes.add(
                    PlcAttribute(
                        id.toString() + UNDERLINE + PANEL_ATTR_IDS[2],
                        0,
                        Constants.VW,
                        ATTR_SET_TEMP,
                        "设定温度"
                    )
                )
                plcAttributes.add(
                    PlcAttribute(
                        id.toString() + UNDERLINE + PANEL_ATTR_IDS[3],
                        0,
                        Constants.VW,
                        ATTR_HUMIDITY,
                        "湿度"
                    )
                )
                databaseRepository.savePlcAttributes(plcAttributes)
            }
        }
    }

    fun deleteRoom(plcRoom: PlcRoom) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                databaseRepository.deleteRoom(plcRoom)
            }
        }
    }

    fun deleteFloor(plcFloor: PlcFloor) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                databaseRepository.deletePlcFloor(plcFloor)
            }
        }
    }

    fun saveFloor(plcFloor: PlcFloor) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                databaseRepository.saveFloor(plcFloor)
            }
        }
    }


    fun getPlcAttributes(attributeIds: Array<String>) =
        databaseRepository.getPlcAttributes(attributeIds).flowOn(Dispatchers.IO).asLiveData()

    fun savePlcSubAttribute(plcSubAttribute: PlcSubAttribute) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                databaseRepository.savePlcSubAttribute(plcSubAttribute)
            }
        }
    }

    fun savePlcAttribute(plcAttribute: PlcAttribute) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                databaseRepository.savePlcAttribute(plcAttribute)
            }
        }
    }

}